﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Web.Framework;
using Job.Entity;
using Job.Logic.Personal;

namespace Job.Web.userControls.Admin
{
    public partial class ViewResumeReceive : BaseCtrl
    {

        private int JobId
        {
            get
            {
                return myR["jobId"].IntValue;
            }
        }

        ApplyJobLogic logic = new ApplyJobLogic();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                tbFromDate.Attributes.Add("onfocus", "calendar()");
                tbEndDate.Attributes.Add("onfocus", "calendar()");
                ddlStatus.SelectedValue = "1";

                BindCount();
                BindData();
            }
        }

        private JobSendResume getSearcher()
        {
            JobSendResume searcher = new JobSendResume();
            searcher.JobId = JobId;
            searcher.SendStatus = ddlStatus.SelectedValue;
            if (tbFromDate.Text.Trim().Length > 0)
            {
                try
                {
                    searcher.SendFrom = Functions.GetDateTime(tbFromDate.Text.Trim());
                }
                catch (Exception ex)
                { }
            }
            if (tbEndDate.Text.Trim().Length > 0)
            {
                try
                {
                    searcher.SendTo = Functions.GetDateTime(tbEndDate.Text.Trim());
                }
                catch (Exception ex)
                { }
            }
            return searcher;
        }

        private void BindCount()
        {
            try
            {
                JobSendResume searcher = getSearcher();
                int count = logic.CountSendByJob(searcher);
                if (count > 0)
                {
                    pagerButtom.RecordCount = count;
                    pagerButtom.DataBind();
                    pagerButtom.Visible = true;
                }
                else
                {
                    pagerButtom.Visible = false;
                }

            }
            catch (Exception ex)
            {
                this.gridSend.Visible = false;
                ShowError("查询接收到的简历列表失败！");
            }
        }
        private void BindData()
        {
            try
            {
                int pageSize = pagerButtom.PageSize;
                int pageIdx = pagerButtom.CurrentPageIndex;
                JobSendResume searcher = getSearcher();
                DataTable dt = logic.GetPageSendByJob(searcher, pageSize, pageIdx);
                if (dt == null)
                {
                    ShowError("未查询到简历列表！");
                }
                else
                {
                    this.gridSend.DataSource = dt;
                    this.gridSend.DataBind();
                }
            }
            catch (Exception ex)
            {
                this.gridSend.Visible = false;
                ShowError("查询接收到的简历失败！");
            }

        }


        private void ShowError(string message)
        {
            this.gridSend.Visible = false;
            this.msgTab.Visible = true;
            this.msgTd.InnerHtml = "<font color='#990000'>" + message + "</font>";
            this.pagerButtom.Visible = false;
        }

        protected void gridSend_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowIndex > -1)
                {
                    try
                    {
                        HyperLink hlResume = (HyperLink)e.Row.FindControl("hlResumeName");
                        Label lbPersonId = (Label)e.Row.FindControl("lbPersonId");
                        Label lbSendId = (Label)e.Row.FindControl("lbSendId");
                        hlResume.NavigateUrl = WebCommon.GetApplicationPath(Request)
                            + "Common/ResumeBrowse.aspx?categoryid=1&resumeid=" + lbPersonId.Text
                            + "&send=" + lbSendId.Text;

                        Label lbStatus = (Label)e.Row.FindControl("lbStatus");
                        String sendStatus = lbStatus.Text.Trim();
                        LinkButton lbtDelete = (LinkButton)e.Row.FindControl("lbtDelete");
                        if (sendStatus.Equals("0"))
                        {
                            lbStatus.Text = "已删除";
                            if (lbtDelete != null) lbtDelete.Visible = false;
                        }
                        else if (sendStatus.Equals("1"))
                            lbStatus.Text = "未阅读";
                        else if (sendStatus.Equals("2"))
                            lbStatus.Text = "已阅读";
                        else
                            lbStatus.Text = "";

                    }
                    catch (Exception ex)
                    { }
                }
            }

        }


        protected void pagerButtom_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }



        protected void gridSend_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int row = e.RowIndex;
            Label lbSendId = (Label)gridSend.Rows[row].FindControl("lbSendId");
            String sendId = lbSendId.Text.Trim();
            logic.UpdateSendStatus(Convert.ToInt32(sendId), '0');

            Label lbStatus = (Label)gridSend.Rows[row].FindControl("lbStatus");
            lbStatus.Text = "已删除";
            LinkButton lbtDelete = (LinkButton)gridSend.Rows[row].FindControl("lbtDelete");
            if (null != lbtDelete) lbtDelete.Visible = false;

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindCount();
            BindData();
        }



    }
}